class Solution(object):
    def friendRequests(self, n, restrictions, requests):
        a_set = set()
        for u, v in restrictions:
            a_set.add((u, v))
        p = [i for i in range(n)]
        p_set = [{i} for i in range(n)]
        def find(x):
            if p[x] == x:
                return p[x]
            else:
                p[x] = find(p[x])
                return p[x]
        def union(x, y):
            rootx = find(x)
            rooty = find(y)
            flag = True
            if rootx != rooty:
                for u, v in a_set:
                    if (u in p_set[rootx] and v in p_set[rooty]) or (u in p_set[rooty] and v in p_set[rootx]):
                        flag = False
                        break
                if flag:
                    if rootx < rooty:
                        p[rooty] = rootx
                        p_set[rootx].update(p_set[rooty])
                        p_set[rooty] = {}
                    else:
                        p[rootx] = rooty
                        p_set[rooty].update(p_set[rootx])
                        p_set[rootx] = {}
            return flag

        ans = []
        for u, v in requests:
            ans.append(union(u, v))
        return ans
            

        



data = Solution()
# n = 3
# restrictions = [[0,1]]
# requests = [[0,2],[2,1]]
# print(data.friendRequests(n, restrictions, requests))
n = 5
restrictions = [[0,1],[1,2],[2,3]]
requests = [[0,4],[1,2],[3,1],[3,4]]
print(data.friendRequests(n, restrictions, requests))